<?php
class VistaComponent extends Component
{
	
/**
Funcion para mostrar imagen el campo requerido
Esta Funcion recibe como parametros:
$div: donde se va a mostrar la imagen
$mensaje: mensaje a mostrar si no se pasa queda como 'Requerido'
**/
function monta_imagem($div,$mensaje=null) 
{
	if($mensaje=='vacio' or empty($mensaje) or !isset($mensaje))$mensaje='Requerido';
	$imagen='/css/tabs/images/default/form/exclamation.gif';
	echo "<script type='text/javascript'> 	if(document.getElementById('".$div."_em') != null ) document.getElementById('".$div."_em').innerHTML='&nbsp;<img src=\'$imagen\' width=\'13px\' height=\'13px\'><font color=\'red\'>&nbsp;<b>$mensaje</b></font>'; </script>";
}


function pr($array) 
{
		$valor="<div id='div_pr'style='display:none;' align='center'><br />
		<table border='2' cellpadding='5' cellspacing='5' width='90%' align='center'>
			<tr bgcolor='#cccccc'><th width='50px'>&nbsp;<b>CAMPO</b></th><th>&nbsp;&nbsp;<b>VALOR</b></th></tr>";
			foreach ($array as $key => $value) 
			{
					$valor.="<tr bgcolor='#ffffff'><td>&nbsp;$key</td><td>&nbsp;&nbsp;$value</td></tr>";
			}
			$valor.="</table>
			<br />
			</div>";

		$valor.="
		<script type='text/javascript'>
				Ext.onReady(function() {
    			Ext.create('Ext.window.Window', {
						//        id : 'mensaje',
        		title : 'IMPRESION DE DATA FORM [\$this->request->data]',
        		layout: 'fit',
        		width: '50%',
        		height: '80%',
    				contentEl: 'div_pr',
        		maximizable: true,
        		collapsed: true,
        		collapsible: true,
						modal: true,
						autoScroll: true,
        		resizable: true,
        		constrain: false
    		}).show();
			});
		</script>";

		$valor.="<script type='text/javascript'>document.getElementById('div_pr').style.display='block';</script>";
		echo $valor;
}


function mensaje($mensaje=null,$titulo=null,$campo=null) 
{
		if(!isset($mensaje))$mensaje='Indique un mensaje';
		if(!isset($titulo))$titulo='MENSAJE';
		$valor="
			<script type='text/javascript'>
			Ext.onReady(function() {
				Ext.MessageBox.alert('".strtr(strtoupper($titulo),"àèìòùáéíóúçñäëïöü","ÀÈÌÒÙÁÉÍÓÚÇÑÄËÏÖÜ")."', '".ucwords(strtolower($mensaje))."', null);

			});
			</script>";
		if(isset($campo))
		{
				$imagen='img/iconos/flecha/flecha_roja.png';
				$valor.="<script type='text/javascript'>
				if(document.getElementById('".$campo."_em') != null ){
				document.getElementById('".$campo."_em').innerHTML='&nbsp;<img src=\'$imagen\' width=\'13px\' height=\'13px\'><font color=\'red\' style=\'cursor:pointer;\'>&nbsp;<b>Error</b></font>';
				document.getElementById('".$campo."').focus();
				}
				</script>";
		}
		echo $valor;
}


function posicion($title)
{
		echo "<script type='text/javascript'>document.getElementById('pasofuncionfilset').innerHTML='$title';</script>";
		echo "<script type='text/javascript'>document.getElementById('pasofuncionfilset').style.display='block';</script>";
		echo "<script type='text/javascript'>document.getElementById('title_navegador').innerHTML='$title / SISTEMA SIMA';</script>";
}


function title($title='')
{
		echo '<script type=\'text/javascript\'>document.getElementById(\'title_'.DLOAD.'\').innerHTML=\'<font class="fondocakeosdsssistema"><b>'.$title.'</b></font>\';</script>';
		echo "<script type='text/javascript'>document.getElementById('title_navegador').innerHTML='$title / ".sistema."';</script>";
}

function tsesion($t=0)
{
		if($t==1)
				echo "<script type='text/javascript'>document.getElementById('msjsessioncerrada').innerHTML='<b>SU SESION A EXPIRADO !!!</b>';</script>";
		else
				echo "<script type='text/javascript'>document.getElementById('msjsessioncerrada').innerHTML='';</script>";
}

function empyt($empyt,$norequeridos=array(),$pre=0) 
{
		$imagen='/css/tabs/images/default/form/exclamation.gif';
		$valor=0;
		foreach ($empyt as $key => $value) 
		{
				if (!in_array($key, $norequeridos)) 
				{
						if($value==null or $value==''or $value==' ')
						{
								if($pre==1)
								{
										if(strpos($key,'_h')>0)
										{
												echo "<script type='text/javascript'>
if(document.getElementById('".$key."_em') != null ){ document.getElementById('".$key."_em').innerHTML='&nbsp;<img src=\'$imagen\' width=\'13px\' height=\'13px\'><font color=\'red\' style=\'cursor:pointer;\'>&nbsp;<b>Requerido</b></font>';
}</script>";
												$valor++;
										}
								}
								else
								{
										echo "<script type='text/javascript'>
if(document.getElementById('".$key."_em') != null ){document.getElementById('".$key."_em').innerHTML='&nbsp;<img src=\'$imagen\' width=\'13px\' height=\'13px\'><font color=\'red\' style=\'cursor:pointer;\'>&nbsp;<b>Requerido</b></font>';}
</script>";
										$valor++;
								}
						}
				}
		}

		if ($valor>=1)$valor=true;else$valor=false;
		return $valor;
}

function limpiar_empyt($empyt) 
{
		$valor=0;
		foreach ($empyt as $key => $value) 
		{
//if (!in_array($key, $norequeridos)) {
				if($value==null or $value=='')
				{
						echo "<script type='text/javascript'>
						document.getElementById('".$key."_em').innerHTML='&nbsp;';
						//document.getElementById('div_pr').style.display='none';
						</script>";
						$valor++;
//}
				}
		}

		if ($valor>=1)$valor=true;
		else $valor=false;
		return $valor;
//echo $valor;
}

function correo($email='vacio',$campo='vacio')
{  
		$imagen='/img/iconos/flecha/flecha_roja.png';
		$valor="";
		if($email=="sxixmxoxnx@gmail.com")
		{
				$valor= true;
				echo "<script type='text/javascript'>
document.getElementById('".$campo."_em').innerHTML='&nbsp;<img src=\'$imagen\' width=\'13px\' height=\'13px\'><font color=\'red\' style=\'cursor:pointer;\'>&nbsp;<b>no pueder ser igual al ejemplo</b></font>';
//document.getElementById('".$campo."').focus();
				</script>";
		}
		else if($email!="")
		{
				if (preg_match('/^[A-Za-z0-9-_.+%]+@[A-Za-z0-9-.]+\.[A-Za-z]{2,4}$/', $email)) 
				{
						$valor= false;
				}
				else
				{
						$valor= true;
						echo "<script type='text/javascript'>
						document.getElementById('".$campo."_em').innerHTML='&nbsp;<img src=\'$imagen\' width=\'13px\' height=\'13px\'><font color=\'red\' style=\'cursor:pointer;\'>&nbsp;<b>Ejemplo sxixmxoxnx@gmail.com</b></font>';
						//document.getElementById('".$campo."').focus();
						</script>";
				}
		}
		else
		{
				$valor= false;
		}
		return $valor;
}

function celular($celular='vacio',$campo='vacio')
{  
		$valor="";
		if($celular!="")
		{
				$codigos=array('0412','0414','0424','0416','0426');
				$mensaje="";
				$celulararray=explode('-',$celular);
				$bf=0;
				if(strlen($celular)<12)
				{
						$mensaje="Error Faltan digitos";
						$valor= true;
						$bf=1;
				}
				else if (!in_array($celulararray[0], $codigos))
				{
						$mensaje="Error codigo no valido ";
						$valor= true;
						$bf=1;
				}
				else
				{
						$valor= false;
				}	

				if($bf==1)
				{
//echo '>>>>>'.$campo."_em";
						$imagen='/img/iconos/flecha/flecha_roja.png';
						echo "<script type='text/javascript'>
document.getElementById('".$campo."_em').innerHTML='&nbsp;<img src=\'$imagen\' width=\'13px\' height=\'13px\'><font color=\'red\' style=\'cursor:pointer;\'>&nbsp;<b>$mensaje</b></font>';
//document.getElementById('".$campo."').focus();
						</script>";
				}
		}
		else
		{
				$valor= false;
		}
		return $valor;
} 




function telefono($telefono='vacio',$campo='vacio')
{  
		$valor="";
		if($telefono!="")
		{
				$codigos=array('0412','0414','0424','0416','0426');
				$mensaje="";
				$telefonoarray=explode('-',$telefono);
				$bf=0;
				if(strlen($telefono)<12)
				{
						$mensaje="Error Faltan digitos";
						$valor= true;
						$bf=1;
				}/*
else if (!in_array($telefonoarray[0], $codigos)){
$mensaje="Error codigo no valido ";
$valor= true;
$bf=1;
		}*/
				else
				{
						$valor= false;
				}
				if($bf==1)
				{
//echo '>>>>>'.$campo."_em";
						$imagen='/img/iconos/flecha/flecha_roja.png';
						echo "<script type='text/javascript'>
document.getElementById('".$campo."_em').innerHTML='&nbsp;<img src=\'$imagen\' width=\'13px\' height=\'13px\'><font color=\'red\' style=\'cursor:pointer;\'>&nbsp;<b>$mensaje</b></font>';
//document.getElementById('".$campo."').focus();
						</script>";
				}
		}
		else
		{
				$valor= false;
		}
		return $valor;
} 


function verifica($model,$campodb,$dato,$campoinput,$id=NULL)
{
		if(isset($model) and isset($campodb) and isset($dato) and isset($campoinput))
		{
			$dato=strtoupper($dato);
				$this->$model = ClassRegistry::init($model);
				if(isset($id))
				{
					$condiciones=array($model.'.id !='=>$id,"$model.$campodb"=>$dato);
				}
				else
				{
					$condiciones=array("$model.$campodb"=>$dato);
				}
				$cant=$this->$model->find('count',array('conditions'=> $condiciones));
				if($cant==0) return false;
				else
				{
						$imagen='/img/iconos/flecha/flecha_roja.png';
						echo "<script type='text/javascript'>
document.getElementById('".$campoinput."_em').innerHTML='&nbsp;<img src=\'$imagen\' width=\'13px\' height=\'13px\'><font color=\'red\' style=\'cursor:pointer;\'>&nbsp;<b>Error ya existe!</b></font>';</script>";
						
						return true;
				}
		}
		return true;
}

function verifica_dos($model,$campodb,$campodbdos,$dato,$datodos,$campoinput,$id=NULL)
{
	
			if(isset($model) and isset($campodb) and isset($dato) and isset($campodbdos) and isset($datodos)  and isset($campoinput))
			{
					$this->$model = ClassRegistry::init($model);
					if(isset($id))
					{
						$condiciones=array($model.'.id !='=>$id,"$model.$campodb"=>$dato,"$model.$campodbdos"=>$datodos);
					}
					else
					{
						$condiciones=array("$model.$campodb"=>$dato,"$model.$campodbdos"=>$datodos);
					}
					$cant=$this->$model->find('count',array('conditions'=> $condiciones));
					if($cant==0) return false;
					else
					{
							$imagen='/img/iconos/flecha/flecha_roja.png';
							echo "<script type='text/javascript'>
	document.getElementById('".$campoinput."_em').innerHTML='&nbsp;<img src=\'$imagen\' width=\'13px\' height=\'13px\'><font color=\'red\' style=\'cursor:pointer;\'>&nbsp;<b>Error ya existe!</b></font>';</script>";
							return true;
					}
			}
			return true;
}
	

function verifica_rif($model,$campodb,$dato,$campoinput,$id=NULL)
{
		if(isset($model) and isset($campodb) and isset($dato) and isset($campoinput))
		{
				$cedulas=explode("-",$dato);
				if(!isset($cedulas[1]))$cedulas[1]=0;
				$this->$model = ClassRegistry::init($model);
				if(isset($id))
				{
					$condiciones=array($model.'.id !='=>$id,"$model.$campodb like "=>"%-".$cedulas[1]);
				}
				else
				{
					$condiciones=array("$model.$campodb like "=>"%-".$cedulas[1]);
				}
				$cant=$this->$model->find('count',array('conditions'=> $condiciones));
				if($cant==0) return false;
				else
				{
						$imagen='/img/iconos/flecha/flecha_roja.png';
						echo "<script type='text/javascript'>
document.getElementById('".$campoinput."_em').innerHTML='&nbsp;<img src=\'$imagen\' width=\'13px\' height=\'13px\'><font color=\'red\' style=\'cursor:pointer;\'>&nbsp;<b>Error ya existe!</b></font>';</script>";
						return true;
				}
		}
		return true;
}





function checkdate($fecha='00-00-0000',$campo='')
{
		$fecha=explode('-',$fecha);
		if(!isset($fecha[0]))$mes='00';else$mes=$fecha[0];
		if(!isset($fecha[1]))$dia='00';else$dia=$fecha[1];
		if(!isset($fecha[2]))$ani='00';else$ani=$fecha[2];

		if(strlen($ani)==4)
		{
				if($ani<1900 or $ani>(date('Y')+100))$ani='00';
				$valor=checkdate($dia,$mes,$ani);
				if($valor==true)
					$valor=false;
				else
				{
						$valor=true;
						$imagen='/img/iconos/flecha/flecha_roja.png';
				echo "<script type='text/javascript'>
				document.getElementById('".$campo."_em').innerHTML='&nbsp;<img src=\'$imagen\' width=\'13px\' height=\'13px\'><font color=\'red\' style=\'cursor:pointer;\'>&nbsp;<b>Error Fecha no Valida</b></font>';
				//document.getElementById('".$campo."').focus();
					</script>";
				}
		}
		else
		{
				$valor=true;
		}
	return $valor;
}

function guardando($mensaje='',$div='guardandoform') 
{
		echo $valor="<style type='text/css'>
.x-message-box .ext-mb-download {
    background: url('../img/guardar/download.gif') no-repeat scroll 6px 0px transparent;
    height: 52px!important;
		}
		</style>

		<script type='text/javascript'>
			Ext.require([
    		'Ext.window.MessageBox',
    		'Ext.tip.*'
			]);
			Ext.onReady(function(){
        Ext.MessageBox.show({
           msg: 'Guardando por Favor Espere...<br ><br >&nbsp;&nbsp;$mensaje  <br >',
           progressText: 'Guardando...',
           width:300,
           wait:true,
           waitConfig: {interval:100},
           icon:'ext-mb-download', //class declarada arriba
           animateTarget: '$div'
       });
        setTimeout(function(){
            Ext.MessageBox.hide();
          //  Ext.MessageBox.msg('Done', 'Your fake data was saved!');
        }, 3000);
		});
		</script>";
}

function cerrar_ventana($id_ven)
{
	echo "<script type='text/javascript'>
	var h = Ext.getCmp('$id_ven');
	Ext.getCmp('$id_ven').destroy();
	h.show ? h.hide() : h.hide();
	h = null;
	$id_ven=null;
	</script>";
}

function empyt_capa($empyt,$requeridos=null,$capa=null,$formulario=null) 
{
	$valor=0;
	foreach ($empyt as $key => $value)
	{
		if (in_array($key, $requeridos)) 
		{
			if($value==null or $value==''or $value==' ')
			{
				$valor++;
			}
		}
	}
	if ($valor>=1)
	{
		$valor=true;
		if($capa=='capa1'){
			$this->activarcapa(1,$formulario);
		}
		else if($capa=='capa2'){
			$this->activarcapa(2,$formulario);
		}
		else if($capa=='capa3'){
			$this->activarcapa(3,$formulario);
		}
	}
	else
	{
		$valor=false;
	}
	return $valor;
}
public function activarcapa($tipo,$formulario=null){
	if($tipo==1){
		echo "<script type='text/javascript'>document.getElementById('title_filset_ventna').innerHTML=' / Datos Personales del Productor';</script>";
        echo "<script type='text/javascript'>document.getElementById('".$formulario."xvista3').style.display='none';</script>";
        echo "<script type='text/javascript'>document.getElementById('".$formulario."xvista2').style.display='none';</script>";
        echo "<script type='text/javascript'>document.getElementById('".$formulario."xvista1').style.display='block';</script>";

		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttona1').style.display='block';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonb1').style.display='block';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonc1').style.display='block';</script>";

		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttona2').style.display='none';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonb2').style.display='none';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonc2').style.display='none';</script>";

		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttona3').style.display='none';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonb3').style.display='none';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonc3').style.display='none';</script>";
	}
	else if($tipo==2){
		echo "<script type='text/javascript'>document.getElementById('title_filset_ventna').innerHTML=' / Datos de Ubicacion de la Vivienda del Productor';</script>";
        echo "<script type='text/javascript'>document.getElementById('".$formulario."xvista3').style.display='none';</script>";
        echo "<script type='text/javascript'>document.getElementById('".$formulario."xvista2').style.display='block';</script>";
        echo "<script type='text/javascript'>document.getElementById('".$formulario."xvista1').style.display='none';</script>";

		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttona1').style.display='none';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonb1').style.display='none';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonc1').style.display='none';</script>";

		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttona2').style.display='block';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonb2').style.display='block';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonc2').style.display='block';</script>";

		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttona3').style.display='none';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonb3').style.display='none';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonc3').style.display='none';</script>";
	}
	else if($tipo==3){
		echo "<script type='text/javascript'>document.getElementById('title_filset_ventna').innerHTML=' / Datos de la Unidad Productiva del Productor';</script>";			
        echo "<script type='text/javascript'>document.getElementById('".$formulario."xvista3').style.display='block';</script>";
        echo "<script type='text/javascript'>document.getElementById('".$formulario."xvista2').style.display='none';</script>";
        echo "<script type='text/javascript'>document.getElementById('".$formulario."xvista1').style.display='none';</script>";

		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttona1').style.display='none';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonb1').style.display='none';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonc1').style.display='none';</script>";

		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttona2').style.display='none';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonb2').style.display='none';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonc2').style.display='none';</script>";

		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttona3').style.display='block';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonb3').style.display='block';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonc3').style.display='block';</script>";
    }
    else if($tipo==4){
		echo "<script type='text/javascript'>document.getElementById('title_filset_ventna').innerHTML=' / Finalizar Registro';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."xvista4').style.display='block';</script>";
	    echo "<script type='text/javascript'>document.getElementById('".$formulario."xvista3').style.display='none';</script>";
	    echo "<script type='text/javascript'>document.getElementById('".$formulario."xvista2').style.display='none';</script>";
	    echo "<script type='text/javascript'>document.getElementById('".$formulario."xvista1').style.display='none';</script>";

		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttona1').style.display='block';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonb1').style.display='none';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonc1').style.display='block';</script>";

		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttona2').style.display='none';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonb2').style.display='none';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonc2').style.display='none';</script>";

		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttona3').style.display='none';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonb3').style.display='block';</script>";
		echo "<script type='text/javascript'>document.getElementById('".$formulario."buttonc3').style.display='none';</script>";
    }
}


function verifica_punto($text,$model,$campodb,$dato,$campoinput,$id=NULL)
{
		if(isset($model) and isset($campodb) and isset($dato) and isset($campoinput))
		{
			$dato=strtoupper($dato);
				
				if(isset($id))
				{
					$condiciones=array($text.'.id !='=>$id,"$text.$campodb"=>$dato);
				}
				else
				{
					$condiciones=array("$text.$campodb"=>$dato);
				}
				$cant=$model->find('count',array('conditions'=> $condiciones));
				if($cant==0) return false;
				else
				{
						$imagen='/img/iconos/flecha/flecha_roja.png';
						echo "<script type='text/javascript'>
document.getElementById('".$campoinput."_em').innerHTML='&nbsp;<img src=\'$imagen\' width=\'13px\' height=\'13px\'><font color=\'red\' style=\'cursor:pointer;\'>&nbsp;<b>Error ya existe!</b></font>';</script>";
						
						return true;
				}
		}
		return true;
}

function verifica_dos_punto($text,$model,$campodb,$campodbdos,$dato,$datodos,$campoinput,$id=NULL)
{
	
			if(isset($model) and isset($campodb) and isset($dato) and isset($campodbdos) and isset($datodos)  and isset($campoinput))
			{
					if(isset($id))
					{
						$condiciones=array($text.'.id !='=>$id,"$text.$campodb"=>$dato,"$text.$campodbdos"=>$datodos);
					}
					else
					{
						$condiciones=array("$text.$campodb"=>$dato,"$text.$campodbdos"=>$datodos);
					}
					$cant=$model->find('count',array('conditions'=> $condiciones));
					if($cant==0) return false;
					else
					{
							$imagen='/img/iconos/flecha/flecha_roja.png';
							echo "<script type='text/javascript'>
	document.getElementById('".$campoinput."_em').innerHTML='&nbsp;<img src=\'$imagen\' width=\'13px\' height=\'13px\'><font color=\'red\' style=\'cursor:pointer;\'>&nbsp;<b>Error ya existe!</b></font>';</script>";
							return true;
					}
			}
			return true;
}
}


